<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../layui/css/layui.css" tppabs="http://res.layui.com/layui/dist/css/layui.css"
          media="all">
    <link rel="stylesheet" href="../layui/css/public.css" media="all" />
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<br/>
<br/>


<form class="layui-form" action="">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">搜索框1</label>
            <div class="layui-input-inline">
                <input type="text" name="cartype" lay-verify="required" placeholder="请输入xx" autocomplete="off"
                       class="layui-input">
            </div>

            <label class="layui-form-label">搜索框2</label>
            <div class="layui-input-inline">
                <input type="text" name="cartype" lay-verify="required" placeholder="请输入xx" autocomplete="off"
                       class="layui-input">
            </div>

            <label class="layui-form-label">搜索框3</label>
            <div class="layui-input-inline">
                <input type="text" name="cartype" lay-verify="required" placeholder="请输入xx" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <button type="button" class="layui-btn layui-btn-normal  layui-icon layui-icon-search" id="doSearch">查询</button>
            <button type="reset" class="layui-btn layui-btn-warm  layui-icon layui-icon-refresh">重置</button>
        </div>

    </div>


</form>


<table class="layui-hide" id="test" lay-filter="test"></table>
</body>


<script src="../layui/layui.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js"></script>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="">头部工具栏1</button>
        <button class="layui-btn layui-btn-sm" lay-event="">头部工具栏2</button>
        <button class="layui-btn layui-btn-sm" lay-event="">头部工具栏3</button>
    </div>
</script>

<script type="text/html" id="barDemo" charset="UTF-8">
    <a class="layui-btn layui-btn-xs" lay-event="">按钮1</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="">按钮2</a>
</script>

<script>
    //编辑动态表格遍历数组
    layui.use('table', function () {
        var table = layui.table;
        table.render({
            elem: '#test'
            , url: 'busCar/getAllCar'
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                , layEvent: 'LAYTABLE_TIPS'
                , icon: 'layui-icon-tips'
            }]
            , title: '车库信息'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'rentid', title: '订单号', edit: 'text',sort: true,align: 'center'}
                , {field: 'price', title: '租金',edit: 'text',sort: true,align: 'center'}
                , {field: 'begindate', title: '开始日期',edit: 'text',sort: true,align: 'center'}
                , {field: 'returndate', title: '返还日期',edit: 'text',sort: true,align: 'center'}
                , {field: 'rentflag', title: '租赁状态',edit: 'text',sort: true,align: 'center'}
                , {field: 'identity', title: '用户身份证号',edit: 'text',sort: true,align: 'center'}
                , {field: 'carnumber', title: '车牌号',edit: 'text',sort: true,align: 'center'}
                , {field: 'opername', title: '用户', edit: 'text',sort: true,align: 'center'}
                , {
                    field: 'createtime', title: '合同生效日期',edit: 'text',sort: true,align: 'center',
                    templet: "<div>{{layui.util.toDateString(d.createtime,'yyyy-MM-dd')}}</div>"
                }

                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150}
            ]]
            , page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
//,curr: 5 //设定初始在第 5 页
                , limit: 3
                , limits: [1, 3, 5]
                , groups: 3 //只显示 3 个连续页码
                , first: true //不显示首页
                , last: false //不显示尾页

            }
        });

//头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
//layer.alert(JSON.stringify(data));
                    var ids = '';
//遍历data
                    for (var i = 0; i < data.length; i++) {
                        ids += "&ids=" + data[i].id;
                    }

                    $.get("user/deleteAll", ids,
                        function (backData) {
                            if ("success" == backData) {
                                table.reload("test", {});//刷新当前页面
                            }
                        });

                    break;
                case 'writeExcel':
                    var data = checkStatus.data;
                    var ids = '';
//遍历data
                    for (var i = 0; i < data.length; i++) {
                        ids += "&ids=" + data[i].id;
                    }

                    location.href = "user/selectAll?ids=" + ids;

                    break;
                case 'getExcel':
                    location.href = "excel/user.xlsx";
                    break;

                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            }
            ;
        });

        table.on('tool(test)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {

                    $.get("user/deleteById/" + data.id,
                        function (backData) {
                            if ("success" == backData) {
                                obj.del();//删除表结构
                                layer.close(index);
                            }
                        });

                });
            } else if (obj.event === 'edit') {

                queryOne(data);

            }
        });
    });
</script>

<script type="application/javascript">

    function queryOne(data) {
        layer.open({
            type: 1,//页面层
            title: ['修改', 'font-size:18px;'],
            area: ['600px', '600px'],
            content: $("#updateUserForm").html(), //这里content是一个普通的String

            success: function () {
                //layer.alert(JSON.stringify(data))
                $("#rentid").val(data.rentid);
                $("#price").val(data.price);
                $("#begindate").val(data.begindate);
                $("#returndate").val(data.returndate);

                if ("在租" == data.rentflag) {
                    $("#rentflag").append("<input type='radio' name='rentflag' value='在租' checked>" + '在租' + "<input type='radio' name='rentflag' value='待租'>" + '待租' + "")
                } else {
                    $("#rentflag").append("<input type='radio' name='rentflag' value='在租' >" + '在租' + "<input type='radio' name='rentflag' value='待租' checked>" + '待租' + "")
                }

                $("#carnumber").val(data.carnumber);
                $("#opername").val(data.opername);
                $("#createtime").val(layui.util.toDateString(data.createtime, 'yyyy-MM-dd'));

                $("#userAddress").val(data.userAddress);

                layui.form.render();

            }
            , btn: ['提交', '取消']
            , yes: function (index, layero) {
                $.ajax({
                    type: "POST",
                    url: "user/update",
                    data: $("#updateUser").serialize(),
                    success: function (backData) {
                        if ("success" == backData) {
                            location.reload();
                        }
                    }
                });


            }
            , btn2: function (index, layero) {
                //alert("取消")
            }

        });
    }


</script>


<script type="text/html" id="updateBillForm">
    <form class="layui-form" id="updateBillManage">

        <input type="hidden" name="id" id="id">

        <div class="layui-form-item">
            <label class="layui-form-label">订单号</label>
            <div class="layui-input-block">
                <input type="text" name="rentid" id="rentid" required lay-verify="required" placeholder="请输入订单号"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">应付金额</label>
            <div class="layui-input-inline">
                <input type="text" name="price" id="price" required lay-verify="required" placeholder="请输入应付金额"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">开始日期</label>
            <div class="layui-input-inline">
                <input type="text" name="begindate" id="begindate" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">返还日期</label>
            <div class="layui-input-inline">
                <input type="text" name="returndate" id="returndate" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">租赁状态</label>
            <div class="layui-input-inline" id="rentflag">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">身份证号</label>
            <div class="layui-input-inline">
                <input type="text" name="identity" id="identity" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">车牌号</label>
            <div class="layui-input-inline">
                <input type="text" name="carnumber" id="carnumber" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">用户</label>
            <div class="layui-input-inline">
                <input type="text" name="opername" id="opername" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">日期</label>
                <div class="layui-input-inline">
                    <input type="date" class="layui-input" name="createdate" id="createdate">
                </div>
            </div>
        </div>

    </form>
</script>

<script>
    layui.use('laydate', function () {
        var laydate = layui.laydate;

        // //常规用法
        // laydate.render({
        //     elem: '#userBirthdate'
        // });
    });
</script>

<script>
    layui.use('upload', function () {
        var upload = layui.upload;

        //执行实例
        var uploadInst = upload.render({
            elem: '#test1' //绑定元素
            , url: 'user/readExcel' //上传接口
            , accept: 'file'
            , done: function (res) {
                if (res.msg == "success") {
                    alert("数据成功导入数据库")
                    //上传完毕回调
                    location.reload();
                    //table.reload("test",{});
                }

            }
            , error: function () {
                //请求异常回调
            }
        });
    });
</script>

</html>